<?php if (!defined("BASEPATH")) exit("No direct script access allowed");

class Profile extends CI_Controller {

     public function index() {
        $this->load->helper("form");
        $this->load->model("ProfileModel");

        $template_data = array();
        $template_data["title"] = "Profile";

        $user_id = $this->uri->segment(2);
        $session_user_id = $this->tank_auth->get_user_id();
        $edit_enabled = false;
         if ($user_id == "" || $user_id == $session_user_id) {
             $edit_enabled = true; //used to check if you may edit this profile
             $user_id = $session_user_id;
         }

        $profile = $this->ProfileModel->getProfileByUserId($user_id);

        if (!$profile) {
            $this->template->view("profiles/profile_not_found", array(), $template_data);
            return;
        }

        $recent_posts = $this->ProfileModel->getRecentPostsByUser($user_id);

        $events_attending = $this->ProfileModel->getEventsAttendingByUser($user_id);

        // create data array to send to the profile view
        $view_data = array();
        $view_data["profile"] = $profile;
        $view_data["recent_posts"] = $recent_posts;
        $view_data["events_attending"] = $events_attending;
        $view_data["error"] = "";
        if ($edit_enabled) {
            $this->load->view("profiles/profile_edit", $view_data, $template_data);
        }
        else {
            $this->load->view("profiles/profile_view", $view_data, $template_data);
        }
    }

    public function save() {
        $this->load->model("ProfileModel");

        $profile = array();
        $profile["country"] = $this->input->post("country");
        $profile["address"] = $this->input->post("address");
        $profile["cellphone_nr"] = $this->input->post("cellphone");
        $user_data["email"] = $this->input->post("email");
        $user_data["display_name"] = $this->input->post("display_name");

        $user_id = $this->tank_auth->get_user_id();
        
        $this->ProfileModel->updateProfileByUserId($user_id, $profile);
        $this->ProfileModel->updateUserDataByUserId($user_id, $user_data);

        redirect("/profile");
    }

    function upload_avatar() {
        $this->load->model("ImagesModel");

        $config["upload_path"] = "./images/uploads/";
        $config["allowed_types"] = "gif|jpg|png";
        $config["max_size"]	= "10000";
        $config["file_name"]  = "image";
        $config["overwrite"]  = false;

        $this->load->library("upload", $config);

        if (!$this->upload->do_upload()) {
            echo $this->upload->display_errors();
        } else {
            $upload_data = $this->upload->data();

            $user_id = $this->tank_auth->get_user_id();
            $this->ImagesModel->saveOrUpdateAvatar($upload_data["file_name"], $upload_data["file_type"], $user_id);

            $resize_config["image_library"] = "gd2";
            $resize_config["source_image"]	= $upload_data["full_path"];
            $resize_config["maintain_ratio"] = true;
            $resize_config["width"] = 100;
            $resize_config["height"] = 100;

            $this->load->library("image_lib", $resize_config);
            
            $this->image_lib->resize();

            redirect("profile");
        }
    }

}